/**
 * 
 */
package gui;

import helden.plugin.werteplugin.PluginZauberInfo;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JScrollPane;

import main.Controller;

import backend.AktiverZauber;

/**
 * @author Christian
 * 
 */
public class UpdateAlleZauberPanel extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2039929588118103782L;
	
	/**
	 * 
	 */
	private JLabel jLabel = null;
	
	/**
	 * 
	 */
	private JLabel jZauberName = null;
	
	/**
	 * 
	 */
	private JPanel jCharsPanel = null; // @jve:decl-index=0:visual-constraint="-165,342"
	
	/**
	 * 
	 */
	private JButton jWeiter = null;
	
	/**
	 * 
	 */
	private JButton jzurueck = null;
	
	/**
	 * 
	 */
	private UpdateZauberController updateController = null;
	
	/**
	 * 
	 */
	private PluginZauberInfo zauber = null;
	
	/**
	 * 
	 */
	private JCheckBox jAlleRepraesentationCheckBox = null;
	
	/**
	 * 
	 */
	private JLabel jLabel1 = null;
	
	/**
	 * 
	 */
	private JScrollPane jScrollPane = null;
	
	/**
	 * 
	 */
	private HashMap<JCheckBox, AktiverZauber> checkBoxen;
	
	/**
	 * 
	 */
	private JButton jAlleWaehlenButton = null;
	
	/**
	 * 
	 */
	private boolean selectToggle = true;
	
	/**
	 * 
	 */
	private JPanel jButtonPanel = null;
	
	/**
	 * This method initializes
	 * 
	 * @param zauber
	 *            Zauber
	 */
	public UpdateAlleZauberPanel(PluginZauberInfo zauber) {

		super();
		checkBoxen = new HashMap<JCheckBox, AktiverZauber>();
		updateController = new UpdateZauberController(this, zauber, checkBoxen);
		this.zauber = zauber;
		initialize();
		jZauberName.setText(zauber.getZauberName() + "   " + zauber.getRepraesentation());
		updateJScrollPane();
	}
	
	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {

		GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
		gridBagConstraints12.gridx = 1;
		gridBagConstraints12.gridwidth = 3;
		gridBagConstraints12.gridy = 4;
		GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
		gridBagConstraints11.gridy = 3;
		gridBagConstraints11.gridwidth = 3;
		gridBagConstraints11.gridx = 1;
		GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
		gridBagConstraints7.gridx = 0;
		gridBagConstraints7.gridwidth = 2;
		gridBagConstraints7.gridy = 2;
		jLabel1 = new JLabel();
		jLabel1.setText("Für alle Repräsentationen anzeigen");
		GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
		gridBagConstraints6.gridx = 2;
		gridBagConstraints6.gridy = 2;
		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
		gridBagConstraints2.gridx = 3;
		gridBagConstraints2.gridy = 2;
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.gridx = 1;
		gridBagConstraints1.gridwidth = 5;
		gridBagConstraints1.gridy = 1;
		jZauberName = new JLabel();
		jZauberName.setText("zaubernaem");
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridy = 0;
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridwidth = 4;
		gridBagConstraints.insets = new Insets(10, 10, 10, 10);
		jLabel = new JLabel();
		jLabel.setText("Auf welche Helden sollen die Werte des Zaubers übertragen werden?");
		this.setLayout(new GridBagLayout());
		this.setSize(new Dimension(662, 463));
		this.add(jLabel, gridBagConstraints);
		this.add(jZauberName, gridBagConstraints1);
		this.add(getJAlleRepraesentationCheckBox(), gridBagConstraints6);
		this.add(jLabel1, gridBagConstraints7);
		this.add(getJScrollPane(), gridBagConstraints11);
		this.add(getJButtonPanel(), gridBagConstraints12);
	}
	
	/**
	 * This method initializes jCharsPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJCharsPanel() {

		if (jCharsPanel == null) {
			jCharsPanel = new JPanel();
			// jCharsPanel.setLayout(new GridBagLayout());
		}
		return jCharsPanel;
	}
	
	/**
	 * This method initializes jWeiter
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJWeiter() {

		if (jWeiter == null) {
			jWeiter = new JButton();
			jWeiter.setText("Weiter");
			jWeiter.addActionListener(updateController);
		}
		return jWeiter;
	}
	
	/**
	 * This method initializes jzurück
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJzurueck() {

		if (jzurueck == null) {
			jzurueck = new JButton();
			jzurueck.setText("zurück");
			jzurueck.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {

					Controller.getGuiController().zeigeHauptfenster();
				}
			});
		}
		return jzurueck;
	}
	
	/**
	 * This method initializes jAlleRepraesentationCheckBox
	 * 
	 * @return javax.swing.JCheckBox
	 */
	public JCheckBox getJAlleRepraesentationCheckBox() {

		if (jAlleRepraesentationCheckBox == null) {
			jAlleRepraesentationCheckBox = new JCheckBox();
			jAlleRepraesentationCheckBox.setSelected(true);
			jAlleRepraesentationCheckBox.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {

					updateJScrollPane();
				}
			});
		}
		return jAlleRepraesentationCheckBox;
	}
	
	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {

		if (jScrollPane == null) {
			jScrollPane = new JScrollPane(getJCharsPanel());
			jScrollPane.setPreferredSize(new Dimension(350, 300));
		}
		return jScrollPane;
	}
	
	/**
	 * Aktualiesiert das Charpanel
	 */
	private void updateJScrollPane() {

		getJCharsPanel().removeAll();
		checkBoxen.clear();
		getJCharsPanel().setLayout(new GridLayout(0, 1));
		String repraesentation = zauber.getRepraesentation();
		if (getJAlleRepraesentationCheckBox().isSelected()) {
			repraesentation = "Alle";
		}
		ArrayList<AktiverZauber> list = Controller.getDatenController()
				.getAlleHeldenMitDemZauber(zauber.getZauberName(), repraesentation);
		for (AktiverZauber aktiveVariante : list) {
			JCheckBox newBox = new JCheckBox();
			String heldenName = Controller.getDatenController().getHeldenNameById(
					aktiveVariante.getHeldenId());
			String rep = aktiveVariante.getZauber().getRepraesentation();
			JPanel panelToAdd = new JPanel();
			panelToAdd.setLayout(new FlowLayout());
			panelToAdd.setSize(new Dimension(300, 80));
			panelToAdd.add(newBox);
			panelToAdd.add(new JLabel(heldenName + "  Rep: " + rep));
			getJCharsPanel().add(panelToAdd);
			checkBoxen.put(newBox, aktiveVariante);
		}
		this.validate();
	}
	
	/**
	 * This method initializes jAlleWaehlenButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJAlleWaehlenButton() {

		if (jAlleWaehlenButton == null) {
			jAlleWaehlenButton = new JButton();
			jAlleWaehlenButton.setText("Alle Auswählen");
			// jAlleWaehlenButton.setEnabled(false);
			jAlleWaehlenButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {

					for (JCheckBox checkBox : checkBoxen.keySet()) {
						checkBox.setSelected(selectToggle);
					}
					if (selectToggle) {
						selectToggle = false;
					} else {
						selectToggle = true;
					}
				}
			});
		}
		return jAlleWaehlenButton;
	}
	
	/**
	 * This method initializes jButtonPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJButtonPanel() {

		if (jButtonPanel == null) {
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints5.gridy = 0;
			gridBagConstraints5.gridx = 2;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 1;
			gridBagConstraints4.gridy = 0;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.insets = new Insets(10, 10, 10, 10);
			gridBagConstraints3.gridy = 0;
			gridBagConstraints3.gridx = 0;
			jButtonPanel = new JPanel();
			jButtonPanel.setLayout(new GridBagLayout());
			jButtonPanel.add(getJzurueck(), gridBagConstraints3);
			jButtonPanel.add(getJWeiter(), gridBagConstraints4);
			jButtonPanel.add(getJAlleWaehlenButton(), gridBagConstraints5);
		}
		return jButtonPanel;
	}
} // @jve:decl-index=0:visual-constraint="251,9"
